<?xml version="1.0" encoding="UTF-8"?>
<!DOCTYPE mapper PUBLIC "-//mybatis.org//DTD Mapper 3.0//EN"
        "http://mybatis.org/dtd/mybatis-3-mapper.dtd">
<!--
    namespace：必须与对应的接口全类名一致
    id:必须与对应接口的某个对应的方法名一致

 -->
<mapper namespace="${daoClass}">

    <!-- 数据库表 -->
    <sql id="table_name">
    ${tableName}
    </sql>

    <!-- 数据库字段 -->
    <sql id="all_columns">
    <#list columns as c>
    ${c.columnName} <#if c_index < ((columns?size) - 1)>,</#if>
    </#list>
    </sql>

    <!-- insert语句 插入的值-->
    <sql id="insert_values">
    <#list columns as c>
        #${r"{"}${c.propertyName} , jdbcType=${c.jdbcType}} <#if c_index < ((columns?size) - 1)>,</#if>
    </#list>
    </sql>

    <!-- set语句更新的字段，全部字段 -->
    <sql id="set_all">
    <#list columns as c>
        <#if c.propertyName !="deleteFlag" && c.propertyName != "createTime">
        ${c.columnName} = #${r"{"}${c.propertyName} , jdbcType=${c.jdbcType}} <#if c_index < ((columns?size) - 1)>
            ,</#if>
        </#if>
    </#list>
    </sql>

    <!-- set语句更新的字段，部分字段 -->
    <sql id="set_partial">
    <#list columns as c>
        <#if c.propertyName !="deleteFlag" && c.propertyName != "createTime">
            <choose>
                <when test="${c.propertyName} != null">
                ${c.columnName} = #${r"{"}${c.propertyName}}<#if c_index < ((columns?size) - 1)>,</#if>
                </when>
                <when test="${c.propertyName}_set_null != null">
                ${c.columnName} = null<#if c_index < ((columns?size) - 1)>,</#if>
                </when>
            </choose>
        </#if>
    </#list>
    </sql>

    <!-- 查询语句的where条件，字符串类型的字段采用like条件 -->
    <sql id="select_where">
        <where>
            delete_flag = 0
        <#list columns as c>
            <choose>
                <when test="${c.propertyName}_set_null == true">
                    and ${c.columnName} is null
                </when>
                <when test="${c.propertyName} != null">
                    and ${c.columnName} <#if c.javaType == "java.lang.String"> like '%' || #${r"{"}${c.propertyName}} ||
                    '%' <#else> = #${r"{"}${c.propertyName}} </#if>
                </when>
            </choose>
        </#list>
        </where>
    </sql>

    <!-- 查询语句的where条件，所有字段都采用 = 方式 -->
    <sql id="select_where_identical">
        <where>
            delete_flag = 0
        <#list columns as c>
            <choose>
                <when test="${c.propertyName}_set_null == true">
                    and ${c.columnName} is null
                </when>
                <when test="${c.propertyName} != null">
                    and ${c.columnName} = #${r"{"}${c.propertyName}}
                </when>
            </choose>
        </#list>
        </where>
    </sql>

    <!-- 对象属性 至 数据库字段映射 -->
    <resultMap id="${modelName}" type="${modelClass}">
    <#list columns as c>
        <#if c.jdbcType != "">
            <result column="${c.columnName}" property="${c.propertyName}" jdbcType="${c.jdbcType}"/>
        <#else>
            <result column="${c.columnName}" property="${c.propertyName}"/>
        </#if>
    </#list>
    </resultMap>

    <!-- 根据条件查询，返回到模型对象 -->
    <select id="select" resultMap="${modelName}">
        SELECT
        <include refid="all_columns"/>
        FROM
        <include refid="table_name"/>
        <choose>
            <when test="findIdentical != null">
                <include refid="select_where_identical"/>
            </when>
            <otherwise>
                <include refid="select_where"/>
            </otherwise>
        </choose>
    </select>

    <!-- 分页查询 由于Mybatis的分页插件是根据SQL ID进行拦截，匹配*.pageQuery的SQL ID将应用分页 -->
    <select id="pageQuery" resultMap="${modelName}">
        SELECT
        <include refid="all_columns"/>
        FROM
        <include refid="table_name"/>
        <choose>
            <when test="findIdentical != null">
                <include refid="select_where_identical"/>
            </when>
            <otherwise>
                <include refid="select_where"/>
            </otherwise>
        </choose>
    </select>

    <!-- 新增 mybatis负责ID的生成-->
    <insert id="insert" parameterType="${modelClass}">
        <selectKey resultType="string" keyProperty="id" order="BEFORE">
            select sys_guid() from dual
        </selectKey>
        INSERT INTO
        <include refid="table_name"/>
        (
        <include refid="all_columns"/>
        )
        VALUES
        (
        <include refid="insert_values"/>
        )
    </insert>

    <!-- 新增 用户负责ID的生成-->
    <insert id="insertWithObjectKey" parameterType="${modelClass}">
        <selectKey resultType="string" keyProperty="id" order="BEFORE">
            select sys_guid() from dual
        </selectKey>
        INSERT INTO
        <include refid="table_name"/>
        (
        <include refid="all_columns"/>
        )
        VALUES
        (
        <include refid="insert_values"/>
        )
    </insert>

    <!-- 根据模型更新全部字段 -->
    <update id="update" parameterType="${modelClass}">
        UPDATE
        <include refid="table_name"/>
        <set>
            <include refid="set_all"/>
        </set>
        WHERE
    ${primaryKey} = #${r"{"}${primaryKeyProperty}}
    </update>

    <!-- 根据Map更新部分字段 -->
    <update id="updateMapModel" parameterType="queryData">
        UPDATE
        <include refid="table_name"/>
        <set>
            <include refid="set_partial"/>
        </set>
        WHERE
    ${primaryKey} = #${r"{"}${primaryKeyProperty}}
    </update>


    <!-- 根据主键逻辑删除-->
    <update id="logicDeleteByPrimaryKey">
        UPDATE
        <include refid="table_name"/>
        SET
        delete_flag = 1,
        update_time = systimestamp
        WHERE
    ${primaryKey} = #${r"{"}${primaryKeyProperty}}
    </update>

    <!-- 根据条件批量逻辑删除-->
    <update id="logicDelete">
        UPDATE
        <include refid="table_name"/>
        SET
        delete_flag = 1,
        update_time = systimestamp
        <include refid="select_where_identical"/>
    </update>

    <!-- 根据主键删除 -->
    <delete id="deleteByPrimaryKey">
        DELETE FROM
        <include refid="table_name"/>
        WHERE
    ${primaryKey} = #${r"{"}${primaryKeyProperty}}
    </delete>

    <delete id="delete">
        DELETE FROM
        <include refid="table_name"/>
        <include refid="select_where_identical"/>
    </delete>

</mapper>